package com.xymtop.Controller;

import com.xymtop.Annotation.Controller;
import com.xymtop.Annotation.Mapping;
import com.xymtop.Annotation.Rest;
import com.xymtop.Entity.Major;
import com.xymtop.Server.ResoultJson;
import com.xymtop.Service.MajorService;
import com.xymtop.Server.Util.DBService;
import com.xymtop.Server.Util.Time;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * @ClassName : MajorController
 * @Description : 专业控制器
 * @Author : 肖叶茂
 * @Date: 2022/12/13  17:42
 */
@Controller(router = "major")
public class MajorController extends HttpServlet {
    private MajorService majorService = new MajorService();

    @Rest
    @Mapping(url = "getList")
    public ResoultJson<Object> getList(HttpServletRequest request, HttpServletResponse response) throws ClassNotFoundException, InstantiationException, IllegalAccessException {
        if(request.getParameter("cid") != null) {
            List<Object> objects = majorService.getList(request.getParameter("cid"));
            if (objects == null) {
                return new ResoultJson<>(204);
            } else {
                return new ResoultJson<>(0, objects, "");
            }
        }else {
           List<Object> objects = DBService.Get("SELECT * FROM `javaweb`.`major`", Major.class.getCanonicalName());
            if (objects == null) {
                return new ResoultJson<>(204);
            } else {
                return new ResoultJson<>(0, objects, "");
            }
        }
    }

    @Rest
    @Mapping(url = "add")
    public ResoultJson<String> add(HttpServletRequest request,HttpServletResponse response) throws ClassNotFoundException, IllegalAccessException {
        String name = request.getParameter("name");
        String detail = request.getParameter("detail");
        String cid = request.getParameter("cid");
        Major major = new Major();
        major.id = Time.getUnixTime();
        major.cid =cid;
        major.detail =detail;
        major.name = name;
        major.code="1";

        if(DBService.InsertObject(major,"major")){
            return new ResoultJson<>(200);
        }else {
            return new ResoultJson<>(204);
        }
    }


}
